<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
    public $username='';
    public $password='';
    public $errorCode ='';
     public function __construct($username, $password) {
        //unset($this->username);
        $this->username = $username;
        $this->password = $password;
    }
	public function authenticate()
	{
            //$users = AlumniUser::model()->find(array('condition'=>" username='{$this->username}' and password='{$this->password}'"));
            $sqlUser ="SELECT au.username,au.alumni_id,a.rename,a.parent_id,a.school_id,a.city_id FROM xyh_alumni_user as au
                    LEFT JOIN xyh_alumni as a on a.id = au.alumni_id
                    WHERE au.username = '$this->username' and password='$this->password'";
            
            $dataArr = Yii::app()->db->createCommand($sqlUser)->queryRow();
            
            if(!$dataArr['username']){
                 $this->errorCode=self::ERROR_PASSWORD_INVALID;
            }
              	 
             else{
                 
                $this->errorCode=self::ERROR_NONE;
                $this->setState('Alumni_id',$dataArr['alumni_id']);
                $this->setState('username',$dataArr['username']);
                $this->setState('rename',$dataArr['rename']);
                $this->setState('parent_id',$dataArr['parent_id']);
                $this->setState('school_id',$dataArr['school_id']);
                $this->setState('city_id',$dataArr['city_id']);
             }
                 
             
//             Yii::app()->session['Alumni_id'] = $dataArr['alumni_id'];
//             Yii::app()->session['username'] = $dataArr['username'];
//             Yii::app()->session['rename'] = $dataArr['rename'];
//             Yii::app()->session['parent_id'] = $dataArr['parent_id'];
//             Yii::app()->session['school_id'] = $dataArr['school_id'];
//             Yii::app()->session['city_id'] = $dataArr['city_id'];
             
              return $this->errorCode;
	}
        
        
        public function Adminauthenticate()
	{
             $users = AdminUser::model()->find(array('condition'=>" username='{$this->username}' and password='{$this->password}'"));
             
           if(!$users['username'])
                $this->errorCode=self::ERROR_PASSWORD_INVALID;
	   else
                  $this->errorCode=self::ERROR_NONE;
             
             Yii::app()->session['admin_id'] = $users['id'];
             Yii::app()->session['Alumni_id'] = 0;
             Yii::app()->session['username'] = $users['username'];
              Yii::app()->session['parent_id'] = -1;
             
             $this->setState('Alumni_id',0);
             $this->setState('admin_id',$users['id']);
             $this->setState('username',$users['username']);
             $this->setState('parent_id',-1);
             
            return $this->errorCode;
	}
    
    
    
//    public function authenticate()
//	{
//		$users=array(
//			// username => password
//			'demo'=>'demo',
//			'admin'=>'admin',
//		);
//		if(!isset($users[$this->username]))
//			$this->errorCode=self::ERROR_USERNAME_INVALID;
//		elseif($users[$this->username]!==$this->password)
//			$this->errorCode=self::ERROR_PASSWORD_INVALID;
//		else
//			$this->errorCode=self::ERROR_NONE;
//		return !$this->errorCode;
//	}
}